<?php
class CombosController extends AppController {

	var $name = 'Combos';

    function beforeFilter () {
        parent::beforeFilter();

		// Aplico el Layout segun la Vista o si es AJAX
		if ($this->RequestHandler->isAjax()) {
			$this->layout = 'ajax';
		}else{
			// Cambio la Vista por la del Rol
			$this->layout = strtolower($this->Session->read('VistaActual'));
		}
    }
	function index() {
		$this->Combo->recursive = 0;

		$combos = $this->Combo->find('all', array(
						'conditions' => array(
											'Combo.client_id' => $this->Session->read('ClientesUsuarioLogueado.id'),
										),
						'order' => array('Combo.name'),
		) );


		$this->set('combos', $combos);
	}

	function view($id = null) {
		if (!$id) {
			$this->Session->setFlash(__('Invalid combo', true));
			$this->redirect(array('action' => 'index'));
		}
		$this->set('combo', $this->Combo->read(null, $id));
	}

	function add() {
		if (!empty($this->data)) {
			$this->Combo->create();

			// Seteo datos que no vienen del Formulario
			$this->Combo->set(array(
				'client_id' => $this->Session->read('ClientesUsuarioLogueado.id'),
			));


			if ($this->Combo->save($this->data)) {
				$this->Session->setFlash(__('El combo ha sido guardado', true));
				$this->redirect('/clients/view/'.$this->Session->read('ClientesUsuarioLogueado.id').'/6');
			} else {
				$this->Session->setFlash(__('The combo could not be saved. Please, try again.', true));
			}
		}
		$typecombos = $this->Combo->Typecombo->find('list');

		// Obtengo el Listado de Profesionales del Cliente
		$users = $this->requestAction('/users/obtenerListadoDoctores/'.$this->Session->read('ClientesUsuarioLogueado.id'));

		$practices = $this->Combo->Practice->find('list');
		$this->set(compact('typecombos', 'users', 'practices'));
	}

	function edit($id = null) {
		if (!$id && empty($this->data)) {
			$this->Session->setFlash(__('Invalid combo', true));
			$this->redirect(array('action' => 'index'));
		}
		if (!empty($this->data)) {
			if ($this->Combo->save($this->data)) {
				$this->Session->setFlash(__('The combo has been saved', true));
				$this->redirect(array('action' => 'index'));
			} else {
				$this->Session->setFlash(__('The combo could not be saved. Please, try again.', true));
			}
		}
		if (empty($this->data)) {
			$this->data = $this->Combo->read(null, $id);
		}
		$typecombos = $this->Combo->Typecombo->find('list');
		$clients = $this->Combo->Client->find('list');
		$users = $this->Combo->User->find('list');
		$practices = $this->Combo->Practice->find('list');
		$this->set(compact('typecombos', 'clients', 'users', 'practices'));
	}

	function delete($id = null) {
		if (!$id) {
			$this->Session->setFlash(__('Invalid id for combo', true));
			$this->redirect(array('action'=>'index'));
		}
		if ($this->Combo->delete($id)) {
			$this->Session->setFlash(__('Combo deleted', true));
			$this->redirect(array('action'=>'index'));
		}
		$this->Session->setFlash(__('Combo was not deleted', true));
		$this->redirect(array('action' => 'index'));
	}
}
?>